﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using TreaShares.Backoffice.DataLayer;

namespace TreaShares.Backoffice.Controlers
{
    [DataObjectAttribute(true)]
    public class EntityManager
    {
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public List<EnitiyObj> Select(string ClientId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                Guid cId = new Guid(ClientId);
                var entities = db.Entities.Where(e => e.ClientId == cId).Select(e => new EnitiyObj() { Id = e.Id, IsActive = e.IsActive, LongName = e.LongName, Parent = (e.Parent.HasValue) ? e.Parent.Value : -1, ShortName = e.ShortName }).ToList();

                foreach (var entity in entities)
                {
                    if (entity.Parent.HasValue)
                    {
                        EnitiyObj entityObj = entities.FirstOrDefault(e => e.Id == entity.Parent.Value);
                        if (entityObj != null)
                        {
                            entity.ParentName = entityObj.ShortName;
                        }
                    }
                }
                return entities;
            }
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public void Delete(int Id)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                db.Entities.DeleteOnSubmit(db.Entities.Where(e => e.Id == Id).Single());
                db.SubmitChanges();
            }
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public void Update(int Id, string shortName, string longName, bool isActive,int parent, string ClientId)
        {

            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                int? pid;
                if (parent == -1) { pid = null; } else { pid = parent; }

                var entity = db.Entities.Where(e => e.Id == Id).Single();
                entity.ShortName = shortName;
                entity.LongName = longName;
                entity.IsActive = isActive;
                entity.ClientId = new Guid(ClientId);
                entity.Parent = pid;
                db.SubmitChanges();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public void InsertEntity(string shortName, string longName, bool isActive, int parentId, string ClientId)
        {
            using (TreasharesDataContext db = new TreasharesDataContext())
            {
                int? pid;
                if (parentId == -1) { pid = null; } else { pid = parentId; }
                db.Entities.InsertOnSubmit(new Entity() { ShortName = shortName, LongName = longName, IsActive = isActive, ClientId = new Guid(ClientId), Parent = pid });
                db.SubmitChanges();
            }
        }
    }
    public class EnitiyObj : Entity
    {
        public string ParentName { get; set; }
    }
}